package gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class NumberInsert extends Panel implements Runnable {
	LinkedList<Character> valueChar = new LinkedList<Character>();
								// Number of the thing itself
	public int pVBefore = 0;	// Multiple of 10's every one number represents 10 past zero
	public int pVAfter = 0;		// 1=10, 2=100, 3=1000,
	String Question="";
	float Num=0;
	String finalAns="";
	
	
	
	public NumberInsert(float Num){this.Num=Num;}
	
	
	Random rand = new Random();
	int numToInsert= rand.nextInt(10);

	
	public void ListConvert() {
		String conNum = Num + "";
		LinkedList<Character> convertedNumber = new LinkedList<Character>();
		for (int i = 0; i < conNum.length(); i++) {
			char A = conNum.charAt(i);
			convertedNumber.add(i, A);
		}
		valueChar = convertedNumber;
	}


	public void decimalFinder() {
		for (int i = 0; i < valueChar.size(); i++) {
			if (valueChar.get(i) == '.') {
				pVBefore = i;
				pVAfter = valueChar.size() - i - 1;
			}
		}
	}
	
	
	public int BeforeD() {
		String place = null;
		int B = rand.nextInt(pVBefore);

		//special cases
		if(B>pVBefore){B=pVBefore;}
		if(B==0){B=4;}
		switch (B) {
		case 1:
			place = "Ones";
			break;
			
		case 2:
			place = "Tens";
			break;
			
		case 3:
			place = "Hundreds";
			break;
			
		case 4:
			place = "Thousands";
			break;
			
		}
		Question=("Enter the number "+(numToInsert)+" in the " + place + " place");
		return B;
	}
	
	public int AfterD() {
		String place = null;
		int B = rand.nextInt(pVAfter);
		
		//special cases
		if(B>pVAfter){B=pVAfter;}
		if(B==0){B=4;}
		switch (B) {
		case 1:
			place = "Tenths";
			break;
			
		case 2:
			place = "Hundredths";
			break;
			
		case 3:
			place = "Thousandths";
			break;
			
		case 4:
			place = "Tenthousandths";
			break;
			
		}
		Question=("Enter the number "+(numToInsert)+" in the " + place + " place");
		return B;
	}

	
	
	
	public void QuestionAnswer() {
		Random rand = new Random();
		
		int inBefore=0, inAfter=0;
		
		int A = rand.nextInt(2);// Determines if the number is going to be
								// before the decimal or after the decimal
		// Determines where after or before the decimal will be
								
		
		if (A == 1) {
			inBefore=BeforeD();
			// Decides where the number should go BEFORE the decimal
			int Answer=pVBefore-inBefore+1;
			valueChar.add(Answer, (char)(numToInsert+48));
			
		} else if (A == 2 || A==0) {
			inAfter=AfterD();
			// Decides where the number should go AFTER the decimal
			int Answer=pVBefore+inAfter;
			valueChar.add(Answer, (char)(numToInsert+48));
		}
		
		for(int i=0;i<pVBefore+pVAfter+1;i++){
			finalAns =finalAns+""+valueChar.get(i);
		}
	}
	
	public String getQuestion(){
		return Question;
	}
	public String getfinalAnswer(){
		return finalAns;
	}
	
	@Override
	public void run() {
		ListConvert();
		decimalFinder();
		QuestionAnswer();
		
	}
		
	

}